package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.service.IWithdrawContractService;
import com.mdd.admin.validate.WithdrawContractCreateValidate;
import com.mdd.admin.validate.WithdrawContractSearchValidate;
import com.mdd.admin.validate.WithdrawContractUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.WithdrawContractDetailVo;
import com.mdd.admin.vo.WithdrawContractListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.WithdrawContract;
import com.mdd.common.mapper.WithdrawContractMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 新建合同请款信息实现类
 * @author LikeAdmin
 */
@Service
public class WithdrawContractServiceImpl implements IWithdrawContractService {
        
    @Resource
    WithdrawContractMapper withdrawContractMapper;

    /**
     * 新建合同请款信息列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<WithdrawContractListedVo>
     */
    @Override
    public PageResult<WithdrawContractListedVo> list(PageValidate pageValidate, WithdrawContractSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<WithdrawContract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        withdrawContractMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:paymentRequestNumber@Payment_request_number:str",
            "=:invoiceNumber@invoice_number:str",
            "=:creator:str",
            "=:currency:str",
            "=:currencyid@currencyId:long",
            "=:agencyDiscountFee@agency_discount_fee:str",
            "=:claimAmount@claim_amount:str",
            "=:officialFee@official_fee:str",
            "=:agencyFee@agency_fee:str",
            "=:originalCurrency@original_currency:str",
            "=:totalOriginalRequest@total_original_request:str",
            "=:originalOfficialFee@original_official_fee:str",
            "=:originalCommissionFee@original_commission_fee:str",
            "=:amountReceived@amount_received:str",
            "=:receivedDate@received_date:str",
            "=:finalProcessingTime@final_processing_time:str",
        });

        IPage<WithdrawContract> iPage = withdrawContractMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<WithdrawContractListedVo> list = new LinkedList<>();
        for(WithdrawContract item : iPage.getRecords()) {
            WithdrawContractListedVo vo = new WithdrawContractListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 新建合同请款信息详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return WithdrawContract
     */
    @Override
    public WithdrawContractDetailVo detail(Integer id) {
        WithdrawContract model = withdrawContractMapper.selectOne(
                new QueryWrapper<WithdrawContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        WithdrawContractDetailVo vo = new WithdrawContractDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 新建合同请款信息新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(WithdrawContractCreateValidate createValidate) {
        WithdrawContract model = new WithdrawContract();

        model.setPaymentRequestNumber(createValidate.getPaymentRequestNumber());
        model.setInvoiceNumber(createValidate.getInvoiceNumber());
        model.setCreator(createValidate.getCreator());
        model.setCurrency(createValidate.getCurrency());
        model.setCurrencyid(createValidate.getCurrencyid());
        model.setAgencyDiscountFee(createValidate.getAgencyDiscountFee());
        model.setClaimAmount(createValidate.getClaimAmount());
        model.setOfficialFee(createValidate.getOfficialFee());
        model.setAgencyFee(createValidate.getAgencyFee());
        model.setOriginalCurrency(createValidate.getOriginalCurrency());
        model.setTotalOriginalRequest(createValidate.getTotalOriginalRequest());
        model.setOriginalOfficialFee(createValidate.getOriginalOfficialFee());
        model.setOriginalCommissionFee(createValidate.getOriginalCommissionFee());
        model.setAmountReceived(createValidate.getAmountReceived());
        model.setReceivedDate(createValidate.getReceivedDate());
        model.setFinalProcessingTime(createValidate.getFinalProcessingTime());

        // 设置 操作人信息 以及 创建时间
        model.setUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setIsDelete(0);

        withdrawContractMapper.insert(model);
    }

    /**
     * 新建合同请款信息编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(WithdrawContractUpdateValidate updateValidate) {
        WithdrawContract model = withdrawContractMapper.selectOne(
                new QueryWrapper<WithdrawContract>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setPaymentRequestNumber(updateValidate.getPaymentRequestNumber());
        model.setInvoiceNumber(updateValidate.getInvoiceNumber());
        model.setCreator(updateValidate.getCreator());
        model.setCurrency(updateValidate.getCurrency());
        model.setCurrencyid(updateValidate.getCurrencyid());
        model.setAgencyDiscountFee(updateValidate.getAgencyDiscountFee());
        model.setClaimAmount(updateValidate.getClaimAmount());
        model.setOfficialFee(updateValidate.getOfficialFee());
        model.setAgencyFee(updateValidate.getAgencyFee());
        model.setOriginalCurrency(updateValidate.getOriginalCurrency());
        model.setTotalOriginalRequest(updateValidate.getTotalOriginalRequest());
        model.setOriginalOfficialFee(updateValidate.getOriginalOfficialFee());
        model.setOriginalCommissionFee(updateValidate.getOriginalCommissionFee());
        model.setAmountReceived(updateValidate.getAmountReceived());
        model.setReceivedDate(updateValidate.getReceivedDate());
        model.setFinalProcessingTime(updateValidate.getFinalProcessingTime());

        // 设置 操作人信息 以及 更新时间
        model.setUpdateUserId(Long.valueOf(LikeAdminThreadLocal.getAdminId()));
        model.setUpdateUserName(LikeAdminThreadLocal.get("username").toString());
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));

        withdrawContractMapper.updateById(model);
    }

    /**
     * 新建合同请款信息删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        WithdrawContract model = withdrawContractMapper.selectOne(
                new QueryWrapper<WithdrawContract>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        withdrawContractMapper.updateById(model);
    }

}
